package it.trick.admin.service;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import it.trick.admin.App;
import it.trick.admin.Constants;
import it.trick.admin.Util;
import it.trick.admin.model.ChapterModel;
import it.trick.admin.model.ContentsBaseModel;
import java.net.URL;

public class ChapterService {

    /**
     * キャプターリストの取得
     * @param chapterKeyString
     * @return
     * @throws Exception
     */
    public static List<ChapterModel> getList(String chapterKeyString) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_CHAPTER_LIST_PATH + chapterKeyString);
        Type collectionType = new TypeToken<Collection<ChapterModel>>() {
        }.getType();

        Gson gson = new Gson();

        List<ChapterModel> modelList = gson.fromJson(json, collectionType);

        return modelList;
    }

    /**
     * 追加
     * @param site
     * @param model
     * @return
     * @throws Exception
     */
    public static ChapterModel addChapter(ContentsBaseModel model) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.ADD_CHAPTER_PATH);
        Type responseModelType = new TypeToken<ChapterModel>() {}.getType();

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("category=");
        psb.append(model.getPrarentKeyString());
        psb.append("&");
        psb.append("name=");
        psb.append(model.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(model.getIndex());

        return (ChapterModel)Http.postGetResponseJson(postURL, psb.toString(), responseModelType);
    }

    /**
     * 更新
     * @param chapterModel
     * @throws Exception
     */
    public static void updateChapter (
            ChapterModel chapterModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.UPDATE_CHAPTER_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("chapter=");
        psb.append(chapterModel.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(chapterModel.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(chapterModel.getIndex());
        psb.append("&");
        psb.append("content=");
        psb.append(chapterModel.getContent());
        psb.append("&");
        psb.append("keyWord=");
        psb.append(Util.keywordListToString(chapterModel.getKeyWordList()));
        psb.append("&");
        psb.append("description=");
        psb.append(chapterModel.getDescription());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * index更新
     * @param chapterModel
     * @throws Exception
     */
    public static void changeChapterIndex (
            ChapterModel chapterModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.CHANGE_CHAPTER_INDEX_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("chapter=");
        psb.append(chapterModel.getKeyValueString());
        psb.append("&");
        psb.append("index=");
        psb.append(chapterModel.getIndex());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 名前更新
     * @param chapterModel
     * @throws Exception
     */
    public static void changeChapterName (
            ChapterModel chapterModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.CHANGE_CHAPTER_NAME_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("chapter=");
        psb.append(chapterModel.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(chapterModel.getName());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 削除
     * @param site
     * @param categoryKeyString
     */
    public static void deleteChapter(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.DELETE_CHAPTER_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("chapter=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 公開・非公開
     * @param categoryKeyString
     */
    public static void publicChapter(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.PUBLIC_CHAPTER_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("chapter=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * キャプターの取得
     * @return
     * @throws Exception
     */
    public static ChapterModel getChapter(String chapterKeyString) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_CHAPTER_PATH + chapterKeyString);
        Type type = new TypeToken<ChapterModel>() {}.getType();

        Gson gson = new Gson();

        ChapterModel model = gson.fromJson(json, type);

        return model;
    }

}
